package com.ating.demo.controller;

import cn.hutool.core.io.FileUtil;
import com.ating.demo.Exceptiom.CustomException;
import com.ating.demo.common.Result;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/files")
public class FilesController {

    private static final String filePath = System.getProperty("user.dir") + "/files/";

    @PostMapping("/upload")
    public Result upload(MultipartFile file){
        String originalFilename = file.getOriginalFilename();

        //如果路径不存在
        if(!FileUtil.isDirectory(filePath)){
            FileUtil.mkdir(filePath);
        }

        //加一个时间戳
        String fileName = System.currentTimeMillis() + originalFilename;
        String realFilePath = filePath + fileName;

        try {
            FileUtil.writeBytes(file.getBytes(),realFilePath);
        } catch (IOException e) {
            e.printStackTrace();
            throw new CustomException("500","上传失败");
        }

        //返回一个网络路径
        String url = "http://localhost:8080/files/download/" + fileName;
        return Result.success(url);
    }

    @GetMapping("/download/{filename}")
    public void download(@PathVariable String filename, HttpServletResponse response){
        try {
            response.addHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(filename, StandardCharsets.UTF_8));
            response.setContentType("application/octet-stream");
            OutputStream os = response.getOutputStream();
            String realPath = filePath + filename;

            //获取文件的字节数组
            byte[] bytes = FileUtil.readBytes(realPath);
            os.write(bytes);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new CustomException("500","下载失败");
        }
    }

    /**富文本上传接口**/
    @PostMapping("/wang/upload")
    public Map<String,Object> wangUpload(MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        if(!FileUtil.isDirectory(filePath)){
            FileUtil.mkdir(filePath);
        }
        String fileName = System.currentTimeMillis() + originalFilename;
        String realFilePath = filePath + fileName;
        try {
            FileUtil.writeBytes(file.getBytes(),realFilePath);
        } catch (IOException e) {
            e.printStackTrace();
            throw new CustomException("500","上传失败");
        }

        String url = "http://localhost:8080/files/upload" + fileName;
        //wang返回的数据参数
        Map<String,Object> map = new HashMap<>();
        List<Map<String,Object>> resultList = new ArrayList<>();
        Map<String,Object> map1 = new HashMap<>();
        map1.put("url",url);
        resultList.add(map1);
        map.put("errno",0);
        map.put("data",resultList);
        return map;
    }
}
